package org.whuims.service.standard;

import org.elasticsearch.common.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.stereotype.Service;
import org.whuims.entity.Standard;
import org.whuims.entity.vo.StandardVo;
import org.whuims.repository.solr.SolrStandardDao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by zhongwei on 16/12/22.
 */
@Service
public class StandardSolrService {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    @Autowired
    private SolrStandardDao solrStandardDao;
    @Autowired
    private SolrTemplate solrTemplate;

    public Page<Standard> findBySearchFields(Pageable pageable, String searchFields) {
        return solrStandardDao.findBySearchFields(pageable, searchFields);
    }

    public Page<Standard> findByTemplate(Pageable pageable, String keyword) {
        Query query = new SimpleQuery();
        query.setPageRequest(pageable);
        query.addCriteria(new Criteria("searchFields").expression(keyword));
        query.addCriteria(new Criteria("valid").expression("1"));
        solrTemplate.setSolrCore("std_std");
        ScoredPage<Standard> scoredPage = solrTemplate.queryForPage(query, Standard.class);
        return scoredPage;
    }

    public Page<Standard> findByTemplate(Pageable pageable, StandardVo standardVo) {
        Query query = null;
        try {
            query = buildQuery(standardVo);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        query.setPageRequest(pageable);
        solrTemplate.setSolrCore("std_std");
        ScoredPage<Standard> scoredPage = solrTemplate.queryForPage(query, Standard.class);
        return scoredPage;
    }

    private Query buildQuery(StandardVo standardVo) throws ParseException {
        Query query = new SimpleQuery();
        String name = standardVo.getName();
        String titleCn = standardVo.getTitleCn();
        String titleEn = standardVo.getTitleEn();
        Date beginPublishDate = standardVo.getBeginPublishDate();
        Date endPublishDate = standardVo.getEndPublishDate();
        Date beginEffectiveDate = standardVo.getBeginEffectiveDate();
        Date endEffectiveDate = standardVo.getEndEffectiveDate();
        String quotas = standardVo.getQuotas();
        if (!Strings.isNullOrEmpty(titleCn)) {
            query.addCriteria(new Criteria("title_cn").expression(titleCn));
        }
        if (!Strings.isNullOrEmpty(titleEn)) {
            query.addCriteria(new Criteria("title_en").expression(titleEn));
        }
        if (!Strings.isNullOrEmpty(name)) {
            query.addCriteria(new Criteria("name").expression(name));
        }
        if (beginPublishDate != null || endPublishDate != null) {
            if (beginPublishDate != null && endPublishDate != null) {
                query.addCriteria(new Criteria("publish_date").between(beginPublishDate, endPublishDate));
            } else if (beginPublishDate != null && endPublishDate == null) {
                query.addCriteria(new Criteria("publish_date").greaterThanEqual(beginPublishDate));
            } else {
                query.addCriteria(new Criteria("publish_date").lessThan(endPublishDate));
            }
        }
        if (beginEffectiveDate != null || endEffectiveDate != null) {
            if (beginEffectiveDate != null && endEffectiveDate != null) {
                query.addCriteria(new Criteria("effective_date").between(beginEffectiveDate, endEffectiveDate));
            } else if (beginEffectiveDate != null && endEffectiveDate == null) {
                query.addCriteria(new Criteria("effective_date").greaterThanEqual(beginEffectiveDate));
            } else {
                query.addCriteria(new Criteria("effective_date").lessThan(endEffectiveDate));
            }
        }
        if (standardVo.getValid() == 0) {
            query.addCriteria(new Criteria("valid").expression("1"));
        }
        if (!Strings.isNullOrEmpty(quotas)) {
            query.addCriteria(new Criteria("quotas").expression(quotas));
        }
        return query;
    }
}
